<template>
  <div>
    <el-form
      :model="supplierForm"
      ref="supplierForm"
      class="demo-ruleForm"
      :inline="true"
      size="small"
    >
      <el-form-item prop="id">
        <el-input
          v-model="supplierForm.id"
          clearable
          @clear="getSupplierList"
          placeholder="请输入ID"
        ></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input
          v-model="supplierForm.name"
          placeholder="请输入供应商名称"
        ></el-input>
      </el-form-item>
      <el-form-item prop="linkman">
        <el-input
          v-model="supplierForm.linkman"
          placeholder="请输入供应商联系人"
        ></el-input>
      </el-form-item>
      <el-form-item prop="mobile">
        <el-input
          v-model="supplierForm.mobile"
          placeholder="请输入供应商电话"
        ></el-input>
      </el-form-item>
      <el-form-item prop="id">
        <el-button type="primary" @click="searchSupplier">查询</el-button>
        <el-button type="primary" @click="addDialog">新增</el-button>
        <el-button @click="resetForm('supplierForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="list" border style="width: 100%">
      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="name" label="供应商名称"> </el-table-column>
      <el-table-column prop="linkman" label="联系人" width="120">
      </el-table-column>
      <el-table-column prop="mobile" label="联系电话"> </el-table-column>
      <el-table-column prop="remark" label="备注"> </el-table-column>
      <el-table-column prop="address" label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="getDialog(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="delSupplier(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[7, 10, 15, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- dialog框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="500px">
      <span>
        <el-form
          :model="supplier"
          :rules="rules"
          ref="supplier"
          label-width="100px"
          class="demo-ruleForm"
          style="width: 400px"
        >
          <el-form-item label="供应商名称" prop="name">
            <el-input
              v-model.trim="supplier.name"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="联系人" prop="linkman">
            <el-input
              v-model.trim="supplier.linkman"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="mobile">
            <el-input
              v-model.trim="supplier.mobile"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model.trim="supplier.remark"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearSupplier('supplier')">取 消</el-button>
        <el-button
          type="primary"
          @click="addSupplier"
          v-if="title == '新增供应商'"
          >确 定</el-button
        >
        <el-button type="primary" @click="editSupplier" v-if="title == '编辑供应商'">编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
    </div>
</template>

<script>
import supplierApi from "../../api/supplier";
export default {
  data() {
    return {
      list: [],
      page: 1,
      size: 7,
      total: 0,
      supplierForm: {
        id: "",
        name: "",
        linkman: "",
        mobile: "",
      },
      dialogVisible: false,
      title: "",
      supplier: {
        name: "",
        linkman: "",
        mobile: "",
        remark: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入供应商名称", trigger: "blur" },
        ],
        linkman: [
          { required: true, message: "请输入供应商联系人", trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入供应商联系电话", trigger: "blur" },
        ],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
      },
    };
  },
  mounted() {},
  created() {
    this.getSupplierList();
  },
  methods: {
    async getSupplierList() {
      try {
        let res = await supplierApi.getSupplierList(
          this.page,
          this.size,
          this.supplierForm
        );
        if (res.data.flag) {
          this.list = res.data.data.rows;
          this.total = res.data.data.total;
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    handleSizeChange(val) {
      this.size = val;
      this.getSupplierList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getSupplierList();
    },
    searchSupplier() {
      this.getSupplierList();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    delSupplier(id) {
      this.$confirm("请确定是否删除该供应商, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            let res = await supplierApi.delSupplier(id);
            if (res.data.flag) {
              this.$message.success(res.data.message);
              this.getSupplierList();
            } else {
              this.$message.error(res.data.message);
            }
          } catch {
            this.$message.error("请求发生错误");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addDialog() {
      this.title = "新增供应商";
      this.dialogVisible = true;
    },
    clearSupplier(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    addSupplier() {
      this.$refs["supplier"].validate(async (valid) => {
        if (valid) {
          try {
            let res = await supplierApi.addSupplier(this.supplier);
            if (res.data.flag) {
              this.$message.success(res.data.message);
              this.getSupplierList();
              this.$refs["supplier"].resetFields();
              this.dialogVisible = false;
            } else {
              this.$message.error(res.data.message);
            }
          } catch {
            this.$message.error("请求发生错误");
          }
        } else {
          this.$message.error("输入内容有误");
        }
      });
    },
    async getDialog(id) {
      this.title = "编辑供应商";
      this.dialogVisible = true;
      try {
        let res = await supplierApi.getSupplier(id);
        if (res.data.flag) {
          this.supplier = res.data.data.rows[0];
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    editSupplier() {
      this.$refs["supplier"].validate(async (valid) => {
        if (valid) {
          try {
            let res = await supplierApi.editSupplier(this.supplier.id, this.supplier);
            if (res.data.flag) {
              this.$message.success(res.data.message);
              this.getSupplierList();
              this.$refs["supplier"].resetFields();
              this.dialogVisible = false;
            } else {
              this.$message.error(res.data.message);
            }
          } catch {
            this.$message.error("请求发生错误");
          }
        } else {
          this.$message.error("输入内容有误");
        }
      });
    },
  },
};
</script>

<style scoped >
.el-form {
  margin-top: 20px;
}
.el-pagination {
  margin-top: 7px;
}
</style>
